﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ComicsEditor
{
    public delegate void ActiveDashedRectChangeHandler(ActiveDashedRect activeDashedRect);

    public class ActiveDashedRect : PageObject
    {
        public Rectangle Rectangle;

        public DashedRect DashedRect;

        public ActivePoint TopLeft;
        public ActivePoint Top;
        public ActivePoint TopRight;
        public ActivePoint Left;
        public ActivePoint Right;
        public ActivePoint BottomLeft;
        public ActivePoint Bottom;
        public ActivePoint BottomRight;

        public event ActiveDashedRectChangeHandler ActiveDashedRectChange;

        public ActiveDashedRect(Page Page, Rectangle Rect) : base(Page)
        {
            Rectangle = Rect;

            DashedRect = new DashedRect(Page, Rectangle);

            TopLeft = new ActivePoint(Page, Point.Empty);
            Top = new ActivePoint(Page, Point.Empty);
            TopRight = new ActivePoint(Page, Point.Empty);
            Left = new ActivePoint(Page, Point.Empty);
            Right = new ActivePoint(Page, Point.Empty);
            BottomLeft = new ActivePoint(Page, Point.Empty);
            Bottom = new ActivePoint(Page, Point.Empty);
            BottomRight = new ActivePoint(Page, Point.Empty);

            TopLeft.ActivePointMove += delegate(ActivePoint activePoint, Point oldCenter, Point newCenter)
            {
                Rectangle.Width += Rectangle.X - newCenter.X;
                Rectangle.Height += Rectangle.Y - newCenter.Y;
                Rectangle.X = newCenter.X;
                Rectangle.Y = newCenter.Y;
                Reset();
            };
            Top.ActivePointMove += delegate(ActivePoint activePoint, Point oldCenter, Point newCenter)
            {
                Rectangle.Height += Rectangle.Y - newCenter.Y;
                Rectangle.Y = newCenter.Y;
                Reset();
            };
            TopRight.ActivePointMove += delegate(ActivePoint activePoint, Point oldCenter, Point newCenter)
            {
                Rectangle.Width = newCenter.X + 1 - Rectangle.X;
                Rectangle.Height += Rectangle.Y - newCenter.Y;
                Rectangle.Y = newCenter.Y;
                Reset();
            };
            Left.ActivePointMove += delegate(ActivePoint activePoint, Point oldCenter, Point newCenter)
            {
                Rectangle.Width += Rectangle.X - newCenter.X;
                Rectangle.X = newCenter.X;
                Reset();
            };
            Right.ActivePointMove += delegate(ActivePoint activePoint, Point oldCenter, Point newCenter)
            {
                Rectangle.Width = newCenter.X + 1 - Rectangle.X;
                Reset();
            };
            BottomLeft.ActivePointMove += delegate(ActivePoint activePoint, Point oldCenter, Point newCenter)
            {
                Rectangle.Width += Rectangle.X - newCenter.X;
                Rectangle.Height = newCenter.Y + 1 - Rectangle.Y;
                Rectangle.X = newCenter.X;
                Reset();
            };
            Bottom.ActivePointMove += delegate(ActivePoint activePoint, Point oldCenter, Point newCenter)
            {
                Rectangle.Height = newCenter.Y + 1 - Rectangle.Y;
                Reset();
            };
            BottomRight.ActivePointMove += delegate(ActivePoint activePoint, Point oldCenter, Point newCenter)
            {
                Rectangle.Width = newCenter.X + 1 - Rectangle.X;
                Rectangle.Height = newCenter.Y + 1 - Rectangle.Y;
                Reset();
            };

            Reset();
        }

        private void Reset()
        {
            DashedRect.Rectangle = Rectangle;

            TopLeft.Center = new Point(Rectangle.X, Rectangle.Y);
            Top.Center = new Point(Rectangle.X + Rectangle.Width/2, Rectangle.Y);
            TopRight.Center = new Point(Rectangle.X + Rectangle.Width - 1, Rectangle.Y);
            Left.Center = new Point(Rectangle.X, Rectangle.Y + Rectangle.Height/2);
            Right.Center = new Point(Rectangle.X + Rectangle.Width - 1, Rectangle.Y + Rectangle.Height/2);
            BottomLeft.Center = new Point(Rectangle.X, Rectangle.Y + Rectangle.Height - 1);
            Bottom.Center = new Point(Rectangle.X + Rectangle.Width/2, Rectangle.Y + Rectangle.Height - 1);
            BottomRight.Center = new Point(Rectangle.X + Rectangle.Width - 1, Rectangle.Y + Rectangle.Height - 1);

            TopLeft.Bounds = new Rectangle(0, 0, Rectangle.Right - 10, Rectangle.Bottom - 10);
            Top.Bounds = new Rectangle(Rectangle.X, 0, 1, Rectangle.Bottom - 10);
            TopRight.Bounds = new Rectangle(Rectangle.X + 10, 0, Page.Width - Rectangle.X - 10, Rectangle.Bottom - 10);
            Left.Bounds = new Rectangle(0, Rectangle.Y, Rectangle.Right - 10, 1);
            Right.Bounds = new Rectangle(Rectangle.X + 10, Rectangle.Y, Page.Width - Rectangle.X - 10, 1);
            BottomLeft.Bounds = new Rectangle(0, Rectangle.Y + 10, Rectangle.Right - 10, Page.Height - Rectangle.Y - 10);
            Bottom.Bounds = new Rectangle(Rectangle.X, Rectangle.Y + 10, 1, Page.Height - Rectangle.Y - 10);
            BottomRight.Bounds = new Rectangle(Rectangle.X + 10, Rectangle.Y + 10, Page.Width - Rectangle.X - 10, Page.Height - Rectangle.Y - 10);

            if (ActiveDashedRectChange != null)
                ActiveDashedRectChange(this);
        }

        public override void Dispose()
        {
            DashedRect.Dispose();
            TopLeft.Dispose();
            Top.Dispose();
            TopRight.Dispose();
            Left.Dispose();
            Right.Dispose();
            BottomLeft.Dispose();
            Bottom.Dispose();
            BottomRight.Dispose();
            base.Dispose();
        }

        public override void Show()
        {
            base.Show();
            DashedRect.Show();
            TopLeft.Show();
            Top.Show();
            TopRight.Show();
            Left.Show();
            Right.Show();
            BottomLeft.Show();
            Bottom.Show();
            BottomRight.Show();
        }

        public override void Hide()
        {
            base.Hide();
            DashedRect.Hide();
            TopLeft.Hide();
            Top.Hide();
            TopRight.Hide();
            Left.Hide();
            Right.Hide();
            BottomLeft.Hide();
            Bottom.Hide();
            BottomRight.Hide();
        }
    }
}
